package com.starchen.optimalselection.admin.controller;

import com.starchen.optimalselection.admin.model.AdminRole;
import com.starchen.optimalselection.admin.service.AdminRoleService;
import com.starchen.optimalselection.common.model.QueryParamsForm;
import com.starchen.optimalselection.common.model.ResultPage;
import com.starchen.optimalselection.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author daxilong
 * @date 2025/3/21 10:32
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping(value = "/admin/role")
public class AdminRoleController {

    @Autowired
    private AdminRoleService adminRoleService;

    @ApiOperation(value = "分页查找角色列表")
    @GetMapping("/page")
    public Result getPage(QueryParamsForm queryParamsForm) {
        ResultPage pageInfo = adminRoleService.getPage(queryParamsForm);
        return Result.success(pageInfo);
    }

    @ApiOperation(value = "根据ID查找角色")
    @GetMapping(value = "/info/{id}")
    public Result info(@PathVariable Long id) {
        AdminRole adminRole = adminRoleService.getById(id);
        return Result.success(adminRole);
    }

    @ApiOperation(value = "角色添加的方法")
    @PostMapping(value = "/save")
    public Result saveSysRole(@RequestBody AdminRole sysRole) {
        boolean success = adminRoleService.save(sysRole);
        if(success)
            return Result.success();
        else
            return Result.error();
    }

    @ApiOperation(value = "角色修改的方法")
    @PutMapping("/update")
    public Result updateSysRole(@RequestBody AdminRole sysRole) {
        boolean success = adminRoleService.updateById(sysRole);
        if(success)
            return Result.success();
        else
            return Result.error();
    }

    @ApiOperation(value = "角色删除的方法")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Long id) {
        boolean success = adminRoleService.removeById(id);
        if(success)
            return Result.success();
        else
            return Result.error();
    }

}
